import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:x_project/components/x_button.dart';
import 'package:x_project/components/x_scroll_view.dart';
import 'package:x_project/res/colors.dart';
import 'package:x_project/res/styles.dart';

/// Created by XieXin on 2021/8/26.
/// 提示对话框
class XPromptDialog {
  static void showInputDialog(
    context, {
    required String title,
    String? content,
    String positiveText = "确定",
    String negativeText = "取消",
    Color? positiveBgColor,
    Color? negativeBgColor,
    Color? positiveTextColor,
    Color? negativeTextColor,
    Function()? onPositive,
    Function()? onNegative,
    bool isHideNegative = false,
  }) {
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return Material(
          type: MaterialType.transparency, //透明类型
          child: WillPopScope(
              //禁用返回键
              child: Center(
                child: Container(
                  margin: EdgeInsets.all(ScreenUtil().setWidth(50)),
                  width: double.infinity,
                  height: ScreenUtil().setHeight(320),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius:
                        BorderRadius.circular(ScreenUtil().setWidth(20)),
                  ),
                  child: XPromptWidget(
                    title: title,
                    content: content,
                    positiveText: positiveText,
                    negativeText: negativeText,
                    positiveBgColor: positiveBgColor,
                    negativeBgColor: negativeBgColor,
                    positiveTextColor: positiveTextColor,
                    negativeTextColor: negativeTextColor,
                    onPositive: onPositive,
                    onNegative: onNegative,
                    isHideNegative: isHideNegative,
                  ),
                ),
              ),
              onWillPop: () async {
                return Future.value(false);
              }),
        );
      },
    );
  }
}

class XPromptWidget extends StatefulWidget {
  final String title;
  final String? content;
  final String positiveText;
  final String negativeText;
  final Color? positiveBgColor;
  final Color? negativeBgColor;
  final Color? positiveTextColor;
  final Color? negativeTextColor;
  final Function()? onPositive;
  final Function()? onNegative;
  final bool isHideNegative;

  const XPromptWidget({
    Key? key,
    required this.title,
    this.content,
    this.positiveText = "确定",
    this.negativeText = "取消",
    this.positiveBgColor,
    this.negativeBgColor,
    this.positiveTextColor,
    this.negativeTextColor,
    this.onPositive,
    this.onNegative,
    this.isHideNegative = false,
  }) : super(key: key);

  @override
  XPromptWidgetState createState() => XPromptWidgetState();
}

class XPromptWidgetState extends State<XPromptWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: Column(
        children: [
          SizedBox(
            width: double.infinity,
            height: ScreenUtil().setWidth(92),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Text(
                  widget.title,
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(36),
                    color: XColors.textColor33,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Positioned(
                  right: ScreenUtil().setWidth(12),
                  child: InkWell(
                    onTap: () => Navigator.of(context).pop(),
                    child: Container(
                      width: ScreenUtil().setWidth(80),
                      height: ScreenUtil().setWidth(80),
                      padding: EdgeInsets.symmetric(vertical: 22.w),
                      child: Icon(
                        const IconData(0xe889, fontFamily: "Icons"),
                        size: 42.w,
                        color: XColors.textColorB1,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              margin: EdgeInsets.symmetric(horizontal: 32.w),
              child: XScrollView(
                children: [
                  Text(
                    widget.content ?? "无",
                    style: TextStyles.getColor55Size28(),
                  ),
                ],
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(
              horizontal: 32.w,
              vertical: 20.w,
            ),
            child: Row(
              children: [
                Offstage(
                  offstage: widget.isHideNegative,
                  child: Expanded(
                    child: XButton(
                      key: const Key("onNegative_dialog_button"),
                      text: widget.negativeText,
                      textColor: widget.negativeTextColor ?? Colors.white,
                      color: widget.negativeBgColor ?? Colors.grey[400],
                      height: 70.h,
                      onPressed: () {
                        Navigator.pop(context);
                        if (widget.onNegative != null) widget.onNegative!();
                      },
                    ),
                  ),
                ),
                Offstage(
                  offstage: widget.isHideNegative,
                  child: SizedBox(width: 32.w),
                ),
                Expanded(
                  child: XButton(
                    key: const Key("onPositive_dialog_button"),
                    text: widget.positiveText,
                    textColor: widget.positiveTextColor ?? Colors.white,
                    color: widget.positiveBgColor ??
                        Theme.of(context).primaryColor,
                    height: 70.w,
                    onPressed: () {
                      Navigator.pop(context);
                      if (widget.onPositive != null) widget.onPositive!();
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
